package com.titleevaluation.controller;

import com.titleevaluation.entity.AcademicOrganizations;
import com.titleevaluation.service.AcademicOrganizationsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/applications/{applicationId}/academic-organizations")
public class AcademicOrganizationsController {

    private final AcademicOrganizationsService academicOrganizationsService;

    public AcademicOrganizationsController(AcademicOrganizationsService academicOrganizationsService) {
        this.academicOrganizationsService = academicOrganizationsService;
    }

    @PostMapping
    public ResponseEntity<AcademicOrganizations> createAcademicOrganizations(@PathVariable Long applicationId, @RequestBody AcademicOrganizations academicOrganizations) {
        AcademicOrganizations createdAcademicOrganizations = academicOrganizationsService.createAcademicOrganizations(applicationId, academicOrganizations);
        return ResponseEntity.ok(createdAcademicOrganizations);
    }

    @GetMapping
    public ResponseEntity<List<AcademicOrganizations>> getAcademicOrganizationsByApplicationId(@PathVariable Long applicationId) {
        List<AcademicOrganizations> academicOrganizations = academicOrganizationsService.getAcademicOrganizationsByApplicationId(applicationId);
        return ResponseEntity.ok(academicOrganizations);
    }

    @GetMapping("/{id}")
    public ResponseEntity<AcademicOrganizations> getAcademicOrganizationsById(@PathVariable Long id) {
        return academicOrganizationsService.getAcademicOrganizationsById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PutMapping("/{id}")
    public ResponseEntity<AcademicOrganizations> updateAcademicOrganizations(@PathVariable Long id, @RequestBody AcademicOrganizations academicOrganizations) {
        AcademicOrganizations updatedAcademicOrganizations = academicOrganizationsService.updateAcademicOrganizations(id, academicOrganizations);
        return ResponseEntity.ok(updatedAcademicOrganizations);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAcademicOrganizations(@PathVariable Long id) {
        academicOrganizationsService.deleteAcademicOrganizations(id);
        return ResponseEntity.noContent().build();
    }
}

